package com.thuanhoang.boo.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.thuanhoang.boo.R;
import com.thuanhoang.boo.model.Channel;

public class ChannelListAdapter extends BaseAdapter {
	private static ArrayList<Channel> channelList;
	private LayoutInflater layoutInflater;
	private Context context;

	public ChannelListAdapter(Context context, ArrayList<Channel> channelList) {
		ChannelListAdapter.channelList = channelList;
		this.layoutInflater = LayoutInflater.from(context);
		this.context = context;
		layoutInflater = LayoutInflater.from(context);
	}

	public Context getContext() {
		return context;
	}

	public int getCount() {
		return channelList.size();
	}

	public Object getItem(int position) {
		return channelList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ChannelViewHolder holder;
		Channel channelItem;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.channel_item, parent,false);
			holder = new ChannelViewHolder();
			holder.channelThumbnail = (ImageView) convertView
					.findViewById(R.id.channel_thumbnail);

			convertView.setTag(holder);
		} else {
			holder = (ChannelViewHolder) convertView.getTag();
		}
		try {
			channelItem = ChannelListAdapter.channelList.get(position);
		} catch (Exception e) {
			return null;
		}


		// Load shop image thumbnail if necessary
		holder.channelThumbnail.setVisibility(View.VISIBLE);
		if (channelItem.getChannelImageLink()== ""
				|| channelItem.getChannelImageLink() == null) {

		} else {	
			ImageLoader.getInstance().displayImage(
					channelItem.getChannelImageLink(),
					holder.channelThumbnail);
		}
		return convertView;
	}

	public static class ChannelViewHolder {
		ImageView channelThumbnail;
	}


}
